package com.gccloud.design.core.mp.meta;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.gccloud.feign.filter.dto.SysUser;
import com.gccloud.idc.security.shiro.UserUtils;
import com.gccloud.starter.common.constant.GlobalConst;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * 自动填充一个公共字段
 *
 * @author liuchengbiao
 * @date 2020-06-16 20:56
 */
@Slf4j
public class StarterMetaObjectHandler implements MetaObjectHandler {

    public StarterMetaObjectHandler() {
        log.info(GlobalConst.Console.LINE);
        log.info("初始化StarterMetaObjectHandler进行通用字段填充");
        log.info(GlobalConst.Console.LINE);
    }

    @Override
    public void insertFill(MetaObject metaObject) {
        SysUser user = UserUtils.getCurrentUser(false);
        if (user != null && metaObject.hasGetter(GlobalConst.EntityField.CREATE_BY)) {
            Object createByValue = metaObject.getValue(GlobalConst.EntityField.CREATE_BY);
            if (createByValue == null) {
                metaObject.setValue(GlobalConst.EntityField.CREATE_BY, user.getId());
            }
        }

        if (metaObject.hasGetter(GlobalConst.EntityField.CREATE_DATE)) {
            Object createDateValue = metaObject.getValue(GlobalConst.EntityField.CREATE_DATE);
            if (createDateValue == null) {
                metaObject.setValue(GlobalConst.EntityField.CREATE_DATE, new Date());
            }
        }

        if (user != null && metaObject.hasGetter("tenantId")) {
            Object tenantIdValue = metaObject.getValue("tenantId");
            if (tenantIdValue == null) {
                metaObject.setValue("tenantId", "");
            }
        }

        if (user != null && metaObject.hasGetter(GlobalConst.EntityField.ORG_ID)) {
            Object orgIdValue = metaObject.getValue(GlobalConst.EntityField.ORG_ID);
            if (orgIdValue == null) {
                metaObject.setValue(GlobalConst.EntityField.ORG_ID, user.getOrgId());
            }
        }

        if (user != null && metaObject.hasGetter(GlobalConst.EntityField.UPDATE_BY)) {
            Object updateByValue = metaObject.getValue(GlobalConst.EntityField.UPDATE_BY);
            if (updateByValue == null) {
                metaObject.setValue(GlobalConst.EntityField.UPDATE_BY, user.getId());
            }
        }

        if (metaObject.hasGetter(GlobalConst.EntityField.UPDATE_DATE)) {
            Object updateDateValue = metaObject.getValue(GlobalConst.EntityField.UPDATE_DATE);
            if (updateDateValue == null) {
                metaObject.setValue(GlobalConst.EntityField.UPDATE_DATE, new Date());
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        SysUser user = UserUtils.getCurrentUser(false);
        if (user != null && metaObject.hasGetter(GlobalConst.EntityField.UPDATE_BY)) {
            Object updateByValue = metaObject.getValue(GlobalConst.EntityField.UPDATE_BY);
            if (updateByValue == null) {
                metaObject.setValue(GlobalConst.EntityField.UPDATE_BY, user.getId());
            }
        }

        if (metaObject.hasGetter(GlobalConst.EntityField.UPDATE_DATE)) {
            Object updateDateValue = metaObject.getValue(GlobalConst.EntityField.UPDATE_DATE);
            if (updateDateValue == null) {
                metaObject.setValue(GlobalConst.EntityField.UPDATE_DATE, new Date());
            }
        }
    }
}
